package com.qxzn.app.personalcenter.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.UserInfoBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.personalcenter.R;
import com.qxzn.app.personalcenter.R2;
import com.qxzn.app.util.AppUtil;
import com.qxzn.app.util.SharePreferenceUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 个人中心
 */
public class PersonalCenterActivity extends BaseActivity {
    @BindView(R2.id.iv_head)
    ImageView ivHead;
    @BindView(R2.id.tv_username)
    TextView tvUsername;
    private StringBuilder promptInfo;
    private Call<UserInfoBean> call;

    @Override
    protected int initLayout() {
        return R.layout.activity_personal_center;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void iniTitleBar() {
        tv_title.setText("个人中心");
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        Glide.with(this).load(SharePreferenceUtil.getPortrait(this)).apply(RequestOptions.bitmapTransform(new CircleCrop()).placeholder(R.mipmap.logo).error(R.mipmap.logo)).into(ivHead);
        getUserInfo();
    }

    @OnClick({R2.id.iv_head, R2.id.iv_edit, R2.id.civ_add_robot, R2.id.civ_to_authorized_users, R2.id.civ_shipping_address, R2.id.civ_face_recognition, R2.id.civ_version_update})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.iv_head) {
            startActivityForResult(new Intent(this, PersonalInfoActivity.class), 1);
        } else if (i == R.id.iv_edit) {
        } else if (i == R.id.civ_add_robot) {
            startActivity(RobotDeviceListActivity.class);
        } else if (i == R.id.civ_to_authorized_users) {
            startActivity(ToAuthUserAddRobotActivity.class);
        } else if (i == R.id.civ_shipping_address) {
            startActivity(ShippingAddressActivity.class);
        } else if (i == R.id.civ_face_recognition) {
            startActivity(FaceIDListActivity.class);
        } else if (i == R.id.civ_version_update) {
            startActivity(VersionUpdateActivity.class);
        } else if (i == R.id.tv_exit) {

        }
    }

    private void getUserInfo() {
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), "{\"username\":\"" + SharePreferenceUtil.getUserName(this) + "\"}");
        call = HttpRequest.getService().getUserInfo(body);
        call.enqueue(new Callback<UserInfoBean>() {
            @Override
            public void onResponse(Call<UserInfoBean> call, Response<UserInfoBean> response) {
                UserInfoBean bean = response.body();
                if (bean != null && bean.getCode() == 200) {
                    List<UserInfoBean.DataBean> userList = bean.getData();
                    if (userList != null && userList.size() > 0) {
                        UserInfoBean.DataBean dataBean = userList.get(0);
                        if (dataBean != null) {
                            tvUsername.setText(dataBean.getNiname());
                            Glide.with(PersonalCenterActivity.this).load(HttpRequest.BASE_URL + dataBean.getHeadportrait()).apply(RequestOptions.bitmapTransform(new CircleCrop()).placeholder(R.mipmap.logo).error(R.mipmap.logo)).into(ivHead);
                            saveUserInfo(dataBean);
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<UserInfoBean> call, Throwable t) {
                AppUtil.showToast(PersonalCenterActivity.this, "网络有点问题，请稍后再试！");
            }
        });
    }

    public void saveUserInfo(UserInfoBean.DataBean bean) {
        SharePreferenceUtil.setPortrait(this, HttpRequest.BASE_URL + bean.getHeadportrait());
        SharePreferenceUtil.setNickname(this, bean.getNiname());
        SharePreferenceUtil.setAge(this, bean.getAge());
        SharePreferenceUtil.setSex(this, bean.getGender() == 1 ? "男" : "女");
        SharePreferenceUtil.setPhone(this, bean.getPhone());
        SharePreferenceUtil.setRoles(this, bean.getRoles());
        SharePreferenceUtil.setAddress(this, bean.getCity());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                getUserInfo();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (call != null) {
            call.cancel();
        }
    }
}
